package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JButton;

import BUS.Server;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ConfigConnectionDialog extends JDialog {

	private JPanel contentPane;
	private JTextField textFieldIp;
	private JTextField textFieldPort;
	private MainScreen parentComponent;
	private JButton btnConfigConnection;
	
	public ConfigConnectionDialog(MainScreen mainScreen)
	{				
		InitComponent();
		this.parentComponent=mainScreen;
		try {
			String MyIp = InetAddress.getLocalHost().getHostAddress().toString();
			textFieldPort.setText(String.valueOf(Server.PORT_SERVER));
			this.textFieldIp.setText(MyIp);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					ConfigConnectionDialog frame = new ConfigConnectionDialog();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public ConfigConnectionDialog() {
		InitComponent();
		textFieldPort.setText(String.valueOf(Server.PORT_SERVER));
	}

	private void InitComponent() {
		setTitle("Config Connection");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 340, 231);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		textFieldIp = new JTextField();
		textFieldIp.setEditable(false);
		textFieldIp.setColumns(10);
		
		textFieldPort = new JTextField();
		textFieldPort.setText("3200");
		textFieldPort.setColumns(10);
		
		JLabel lblUsername = new JLabel("Ip:");
		
		JLabel lblIp = new JLabel("Port:");
		
		btnConfigConnection = new JButton("Set Connection");
		btnConfigConnection.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnConfigConnection_actionPerformed();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(25)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblUsername)
						.addComponent(lblIp, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
					.addGap(28)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(textFieldPort, GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
						.addComponent(textFieldIp))
					.addGap(50))
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(123, Short.MAX_VALUE)
					.addComponent(btnConfigConnection)
					.addGap(68))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(33)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblUsername)
						.addComponent(textFieldIp, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblIp)
						.addComponent(textFieldPort, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(34)
					.addComponent(btnConfigConnection)
					.addContainerGap(35, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}

	protected void btnConfigConnection_actionPerformed() {
		this.setVisible(false);
		this.dispose();		
		parentComponent.setPortAtServer(Integer.parseInt(textFieldPort.getText()));
	}
}
